﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FluentValidation;
using Microhard.ApproveFlow.Buz.Models;
using Microhard.ApproveFlow.Buz.Validators;
using Microhard.Core.DBUtility;
using Microhard.ApproveFlow.Buz.Interfaces;
using Microhard.Core;


namespace Microhard.ApproveFlowDesigner
{
    public partial class frmFlowType : Form
    {
        private IApproveFlowType iaft = IoC.Resolve<IApproveFlowType>();
        public static bool IsShow;

        public frmFlowType()
        {
            InitializeComponent();

            DataGridViewButtonColumn btnSave = new DataGridViewButtonColumn();
            btnSave.Text = "保存";
            btnSave.Name = "btnSave";
            btnSave.HeaderText = "保存";
            btnSave.UseColumnTextForButtonValue = true;
            this.dgvFlowTypeList.Columns.Add(btnSave);

            DataGridViewButtonColumn btndelete = new DataGridViewButtonColumn();
            btndelete.Text = "删除";
            btndelete.Name = "btnDelete";
            btndelete.HeaderText = "删除";
            btndelete.UseColumnTextForButtonValue = true;
            this.dgvFlowTypeList.Columns.Add(btndelete);


            this.dgvFlowTypeList.AutoGenerateColumns = false;
            this.dgvFlowTypeList.DataSource = iaft.Query(null);
        }


        private void frmFlowType_FormClosing(object sender, FormClosingEventArgs e)
        {
            IsShow = false;
        }

        private void dgvFlowType_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            DataGridView dgv = sender as DataGridView;
            var row = dgv.Rows[e.RowIndex];
            var col = row.Cells[e.ColumnIndex];
            ExecutedResult vr = null;
            var data = row.DataBoundItem as ApproveFlowTypeModel;
            string vle = col.Value.ToString();

            if ("保存" == vle)
            {
                var validator = new ApproveFlowTypeValidator();
                data.TypeName = row.Cells["ColTypeName"].Value.ToString();
                data.TypeRemark = row.Cells["ColTypeRemark"].Value.ToString();
                var vvr = validator.Validate(data, ruleSet: "Update");
                if (vvr.IsValid)
                {
                    vr = iaft.Update(data);
                }
            }
            else if ("删除" == vle)
            {
                List<Guid> ids = new List<Guid>();
                ids.Add(data.TypeId);
                vr = iaft.Delete(ids);
            }
            if (!vr.IsSucceed)
            {
                MessageBox.Show(vr.ErrorMsgTxt);
            }
            else
            {
                this.dgvFlowTypeList.DataSource = iaft.Query(null);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var data = new ApproveFlowTypeModel
            {
                TypeName = this.txbTypeName.Text,
                TypeRemark = this.txbTypeName.Text
            };
            var validator = new ApproveFlowTypeValidator();
            var vvr = validator.Validate(data, ruleSet: "Insert");
            var ex = new ExecutedResult();
            if (vvr.IsValid)
            {
                ex = iaft.Add(data);
            }
            if (!ex.IsSucceed)
            {
                MessageBox.Show(ex.ErrorMsgTxt);
            }
            else
            {
                this.dgvFlowTypeList.DataSource = iaft.Query(null);
                this.txbTypeName.Text = string.Empty;
                this.txbMark.Text = string.Empty;
            }
        }

    }
}
